﻿Imports System.Data.OleDb
Public Class AddAlbum
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btn_AddAlbum_Click(sender As Object, e As EventArgs) Handles btn_AddAlbum.Click

        Dim newFileName As String = ""
        'Save picture to has disk if it is not empty and has the correct format.
        If (f_Picture.HasFile) Then
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid()
            Dim ext As String = IO.Path.GetExtension(f_Picture.FileName)
            newFileName = myGUID.ToString() + ext
            If isPicture(ext) Then
                Dim picLocationOnHardDiskServer = Request.MapPath("Albums/Covers") & "/" & newFileName
                f_Picture.SaveAs(picLocationOnHardDiskServer)
            Else
                Response.Redirect("AddAlbum.aspx")
            End If
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim sqlString As String = "INSERT INTO Albums(Tourist, AlbumName, Description, Picture, DateAdded) VALUES (@f1, @f2, @f3, @f4, Now())"
        Dim cmd As OleDbCommand = New OleDbCommand(sqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_AlbumName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Description.Text)
        'If member has added a file to use as album picture then use this file, if not use a default image
        If (f_Picture.HasFile) Then
            cmd.Parameters.AddWithValue("@f4", "Albums/Covers/" & newFileName)
        Else
            cmd.Parameters.AddWithValue("@f4", "Images/AlbumCover.png")
        End If
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        'Find id of the new album to use it in order to store the pictures in the addPicturesToAlbum page.
        Dim getLastIdCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM Albums ORDER BY ID DESC", oleDbConn)
        Dim dr = getLastIdCmd.ExecuteReader()
        dr.Read()
        Dim lastId = dr.GetValue(0)
        oleDbConn.Close()
        Response.Redirect(String.Format("AddPicturesToAlbum.aspx?Album={0}", lastId))

    End Sub

    'Check if picture uploaded is a valid picture.
    Private Function isPicture(ByVal pictureExtension As String) As Boolean
        Select Case pictureExtension
            Case ".jpg"
                Return True
            Case ".JPG"
                Return True
            Case ".jpeg"
                Return True
            Case ".JPEG"
                Return True
            Case ".png"
                Return True
            Case ".PNG"
                Return True
            Case ".bmp"
                Return True
            Case ".BMP"
                Return True
            Case ".gif"
                Return True
            Case ".GIF"
                Return True
            Case Else
                Return False
        End Select
    End Function

    Protected Sub btn_Cancel_Click(sender As Object, e As EventArgs) Handles btn_Cancel.Click
        Response.Redirect(String.Format("Profile.aspx?Tourist={0}", User.Identity.Name))
    End Sub
End Class